package com.donleo.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author liangd
 * date 2020-12-08 14:28
 * code 二、通过配置类+
 *
 */
//@Configuration
public class SecurityConfigDemo extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        /*
            1、BCryptPasswordEncoder官方推荐的密码解析器
            2、BCryptPasswordEncoder 是对 bcrypt 强散列方法的具体实现。是基于 Hash  算法实现的单
            向加密。可以通过 strength 控制加密强度，默认  10.
         */
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode("123456");
        auth.inMemoryAuthentication().withUser("liangd").password(password).roles("admin");
    }

    /**
     * 要用到PasswordEncoder，需要将这个类装进ioc容器类
     * 不然会报java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
     */
    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
